<div class="fsh-rightPanel">
    <div class="layui-anim layui-anim-upbit">
        <div id="list_form" class="layui-form-item wx-search">
            <div class="layui-inline">
                <label>
                    <input type="text" name="title" placeholder="请输入名称" autocomplete="off" class="layui-input">
                </label>
            </div>
            <button class="layui-btn" id="search">搜索</button>
            <div class="layui-btn-container" style="text-align: right">
                <button class="layui-btn" id="role_add">新增</button>
            </div>
        </div>
        <table id="list_table" class="layui-hide" lay-filter="mainList"></table>
    </div>
</div>
<script type="text/html" id="toolbarDemo">
<!--    <div class="layui-btn-container">-->
<!--        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>-->
<!--        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>-->
        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</button>
        <button class="layui-btn layui-btn-sm" id="table-imp">导入</button>
<!--    </div>-->
</script>
<script type="text/html" id="barDemo">
    <a class="font-primary" lay-event="detail">查看</a>
    <a class="font-primary" lay-event="update">编辑</a>
<!--    <a class="font-primary" lay-event="del">删除</a>-->
</script>

<script>
    var parentId;
    layui.use(['layer', 'table','upload'], function () {
        var layer = layui.layer;
        var table = layui.table;
        var form = layui.form;
        var upload = layui.upload;

        var tableObj = table.render({
            elem: '#list_table',
            url: context + '/api/qQuestion/list'
            , where: util.getFormData('list_form'),
            //开启头部工具栏，并为其绑定左侧模板，
            toolbar: '#toolbarDemo',
            defaultToolbar: ['print'],
            request: {
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            align: 'center'
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left', title:'全选',width: 100},
                {field: 'title', title: '题目', align: 'center'},
                {field: 'content', title: '内容', align: 'center'},
                {field: 'answer', title: '正确答案', align: 'center'},
                {field: 'type', title: '类别', align: 'center', template: function(d){return d.type==1?'单选': d.type==2?'多选':'判断'}},
                {field: 'createDate', title: '创建日期', align: 'center', width: 200},
                {field: 'handler', title: '操作', align: 'center', width: 150, toolbar: '#barDemo'}
            ]]
            , page: true
        });
        //头工具栏事件
        table.on('toolbar(mainList)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    break;
                case 'getCheckLength':
                    break;
                case 'del':
                    var data = checkStatus.data;
                    if(data.length == 0){
                        showAlert("请选择");
                        return;
                    }
                    layer.confirm('确认删除？', {
                        icon: 7,
                        title: "提示",
                        btn: ['确认', '取消'] //按钮
                    }, function () {
                        var ids = [];
                        for(var i=0;i<data.length;i++){
                            ids[i] = data[i].question_id;
                        }
                        ajax({
                            url: context + '/api/qQuestion/del',
                            data: JSON.stringify(ids),
                            type: 'DELETE',
                            contentType: 'application/json;charset-UTF-8',
                            success: function (result) {
                                console.log(result);
                                if (result.success) {
                                    layer.close();
                                    layer.msg(result.msg);
                                    //刷新列表
                                    reloadTable(tableObj);
                                } else {
                                    showAlert(result.errorMsg||result.msg);
                                }
                            }
                        });
                    });
                    break;
                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_IMP':
                    //layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            }
        });
        var uploadInst = upload.render({
            elem: '#table-imp' //绑定元素
            ,url: context +'/api/qQuestion/addByExcel' //上传接口
            //,acceptMime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            ,exts: 'xls|xlsx'
            ,done: function(res, index, upload){
                if(res.success){
                    showAlert("成功: "+res.data.successNum + ",失败: " + res.data.failsNum);
                    reloadTable(tableObj);
                }else{
                    showAlert(res.msg);
                }
            }
            ,error: function(){
                showAlert('网络异常');
                //请求异常回调
            }
        });
        // 检索
        $("#search").click(function () {
            reloadTable(tableObj);
        });
        // 添加
        $("#role_add").click(function () {
            showDialog({
                title: '新建'
                , template: 'add'
                , tableObj: tableObj
                , saveUrl: context + '/api/qQuestion/add'
                , type: "post"
                , dataType: 'json'
                , height: 650
            })
        });

        //表格内部操作按钮监听
        table.on('tool(mainList)', function (obj) { //注：tool是工具条事件名，mainList是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值

            if (layEvent === 'update') {
                showDialog({
                    title: '编辑 - ' + data.schoolName
                    , template: 'add'
                    , saveUrl: context + '/api/qQuestion/update'
                    , tableObj: tableObj
                    , htmlData: data
                    , type: "patch"
                    , dataType: 'json'
                    , height: 650
                })
            }else if (layEvent === 'detail') {
                showDialog({
                    title: '查看高校 - '+ data.schoolName
                    , type: 'get'
                    , template: 'add'
                    , htmlData: data
                    , height: 650
                })
            }else if(layEvent === 'del'){confirmDel(context + "/api/qQuestion/del/" + data.question_id , tableObj);}
        });
    });
</script>
<style>
    img.fans-portrait {width: 50px;height: 50px;}
    .fsh-rightPanel .layui-table-body .layui-table-cell {height: 50px;line-height: 50px; }
</style>